package org.lonetree.tc.webapp.actions;

import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.lonetree.tc.core.Book;
import org.lonetree.tc.core.User;
import org.lonetree.tc.core.Book.Isbn;;

public class RemoveMyBook extends SecureAction {

	@Override
	protected ActionForward executeSecure(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
	try{
		User curUser = getCurrentUser();
		Isbn isbn = new Isbn(request.getParameter("isbn"));
		Set<Book> books = curUser.getMyBooks();
		for(Iterator<Book> it = books.iterator(); it.hasNext();){
			Book b = it.next();
			if(b.getIsbn().equals(isbn)){
				it.remove();
				//userDAO.updateUser(curUser); // TODO put this back in
				break;
			}
		}
		
		return mapping.findForward("success");
	}
	catch(Exception e){
		// TODO Auto-generated method stub
		return mapping.findForward("failure");
	}
	}

}
